<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>

        let ary = [1, 2, 3, 1, 2, 1, 2, 3, 2, 1, 2, 3]
        //1.创建一个空对象
        let obj = {}
        //2.循环数组中的每一项，把每一项向对象中进行存储=>item:item
        for (let i = 0; i < ary.length; i++) {
            let item = ary[i]
            //3.每一次存储之前进行判断：验证obj是否存在这一项
            if (obj[item] === item) {
                ary[i] = ary[ary.length - 1]//如果索引i这项已经存在，就把数组最后一项替换到当前项，再ary.length删除
                ary.length--
                i--//因为和最后一项替换了，不能跳过替换的这一项，所以i--
            } else {
                obj[item] = item
            }

        }

        // function unique(ary) {
        //     let obj = {}
        //     for (let i = 0; i < ary.length; i++) {
        //         var item = ary[i]
        //         if (obj[item] === item) {
        //             ary[i] = ary[ary.length - 1]
        //             ary.length--
        //             i--
        //         } else {
        //             obj[item] = item
        //         }
        //     }
        //     return obj
        // }

    </script>
</body>

</html>